//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.link;

import java.util.Date;

import tv.dyndns.kishibe.client.Sanitize;
import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.Utility;
import tv.dyndns.kishibe.client.packet.PacketLinkData;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetLinkData extends VerticalPanel implements ClickHandler {
	private final Button buttonUpdate = new Button("修正", this);
	private final Button buttonRemove = new Button("削除", this);
	private final PacketLinkData linkData;
	private final PanelLink panelLink;

	public WidgetLinkData(PacketLinkData linkData, PanelLink panelLink) {
		this.linkData = linkData;
		this.panelLink = panelLink;

		final String url = Sanitize.getInstance().uri(linkData.url);
		{
			final String homePageName = Sanitize.getInstance().html(linkData.homePageName);
			final String authorName = Sanitize.getInstance().html(linkData.authorName);
			final String lastUpdate = Utility.getInstance().toDateFormat(new Date(linkData.lastUpdate));
			String header = "<a href='" + url + "' target='_blank'>" + homePageName + "</a> / " + authorName + " - 最終更新:" + lastUpdate;
			add(new HTML(header));
		}

		{
			final HorizontalPanel panel = new HorizontalPanel();
			panel.setVerticalAlignment(ALIGN_MIDDLE);

			final String bannerUrl = Sanitize.getInstance().uri(linkData.bannerUrl);
			String image = "<a href='" + url + "' target='_black'><img src='" + bannerUrl + "' /></a>";
			panel.add(new HTML(image));

			final String description = Sanitize.getInstance().html(linkData.description);
			final HTML htmlDescription = new HTML(description);
			htmlDescription.setWidth("800px");
			panel.add(htmlDescription);

			{
				final VerticalPanel panelButtons = new VerticalPanel();
				panelButtons.add(buttonUpdate);
				panelButtons.add(buttonRemove);
				panel.add(panelButtons);
			}

			add(panel);
		}
	}

	private final AsyncCallback<Object> callbackRemoveLinkData = new AsyncCallback<Object>() {
		public void onSuccess(Object result) {
			panelLink.reload();
		}

		public void onFailure(Throwable caught) {
			caught.printStackTrace();
		}
	};

	private void setEnabled(boolean enabled) {
		buttonUpdate.setEnabled(enabled);
		buttonRemove.setEnabled(enabled);
	}

	@Override
	public void onClick(ClickEvent event) {
		final Widget sender = (Widget) event.getSource();
		if (sender == buttonUpdate) {
			panelLink.onClickUpdate(linkData);
		} else if (sender == buttonRemove) {
			if (Window.confirm("このリンクを削除しますか？")) {
				setEnabled(false);
				Service.Util.getInstance().removeLinkData(linkData.id, callbackRemoveLinkData);
			}
		}
	}
}
